<?php
namespace Home\Controller;
use Think\Controller;
class PhotoController extends BaseController {
    //崔晓真 晒图列表
    public function photolist(){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;

        $photos = M('photos');
        $qrcodenum=$photos->count();
        $Page = new \Think\Page($qrcodenum,3);
        $Page->setConfig('header', '共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;
    每页<b>3</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme',' %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->pageNav=$Page->show();//在页面{pageNav}
        //$this->link=$Page->show();
        $this->qrcodeList=$photos->limit($Page->firstRow,3)->order('photo_id desc')->select();
        $this->display();
    }

    // 崔晓真 删除晒图
    public function delete($photo_id){
        $photosmodel=M('photos');
        $res=$photosmodel->where("photo_id=$photo_id")->find();
        $photoPath = realpath("./Public/upload/photopic/".$res['photo1']);
        unlink($photoPath);
        //$res=$usersmodel->where("id=$id")->delete();
        if ($photosmodel->delete($photo_id)) {
            $this->success('删除成功！');
        } else {
                $this->error('删除失败');
        }
    }

    //崔晓真 晒图详情
    public function detail($photo_id){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;
        
        $photosModel= M('photos as p')->join('user as u on u.user_id=p.user_id');//实例化一个model对象
        $res=$photosModel->where("photo_id = $photo_id")->select();//asc升序 desc降序
        $this->detail=$res;

        // $commentmodel = M('comments as c')->join('user as u on u.user_id=c.user_id');
        // $qrcodenum=$commentmodel->count();
        // $Page = new \Think\Page($qrcodenum,4);
        // $Page->setConfig('header', '第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
        // $Page->setConfig('prev','上一页');
        // $Page->setConfig('next','下一页');
        // $Page->setConfig('last','末页');
        // $Page->setConfig('first','首页');
        // $Page->setConfig('theme',' %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        // $this->pageNav=$Page->show();//在页面{pageNav}
        // //$this->link=$Page->show();
        // $this->qrcodeList=$commentmodel->where("photo_id = $photo_id")->limit($Page->firstRow,4)->order('comment_id desc')->select();

        // $this->display();

        $commentmodel = M('comments as c')->join('user as u on u.user_id=c.user_id');
        $this->comment=$commentmodel->where("photo_id = $photo_id")->select();
        $this->display();
    }

    // 崔晓真 删除评论
    public function deletecomment($comment_id){
        $commentsmodel=M('comments');
        $res=$commentsmodel->where("comment_id=$comment_id")->find();
        // $photoPath = realpath("./Public/upload/commentpic/".$res['photo']);
        // unlink($photoPath);
        //$res=$usersmodel->where("id=$id")->delete();
        if ($commentsmodel->delete($comment_id)) {
            $this->success('删除成功！');
        } else {
                $this->error('删除失败');
        }
    }

}